require('./main.less');
require('src/main.js');
const { getMessageCode, doRegister } = require('doapis/user/user.js');
const { getPhonesCode } = require('doapis/others/others.js');

$(document).ready(function() {
    let type = 2;               // 1、邮箱；2、手机
    let cannotSend = false;     // 是否正在发送验证码
    let setCount;               // 验证码倒计时
    let codeText = {            // 获取验证码的手机和验证码的值（坑：验证码前端判断）
        code: '',
        phone: ''
    };
    const memberid = $.$getQueryParams('memberid');
    
    if (memberid) {
        $('#shareCode').val(memberid);
        $('#shareCode').removeClass('not_val');
        // 验证码过长，字体缩小
        if (memberid.length >= 13) {
            $('.invite_code').addClass('long');
        }
    }

    const selectCode = $('#selectCode');
    const accountIMG = selectCode.parent().find('img');
    let areaCode = '';
    getPhonesCodeData();


    // 获取手机号区域码
    function getPhonesCodeData(params) {
        getPhonesCode(params, function(res) {
            let html = '';
            const templete = $('#areaBOX .content-box');
            for (let item of res.data.records) {
                html += `
                    <li class="list cl">
                        <span class="name f_l overflow_1">${item.znName}（${item.enName}）</span>
                        <span class="code f_r text_r">${item.areaCode}</span>
                    </li>
                `;
            }
            templete.html('');
            templete.append(html);

            // 初始化区域码
            const templeteLIST = templete.find('.list');
            const firstAreaCode = templeteLIST.find('.code').eq(0).text();
            selectCode.text(firstAreaCode);
            areaCode = firstAreaCode;
            // 设置区域码列表的点击事件
            templeteLIST.click(function() {
                areaCode = $(this).find('.code').eq(0).text();
                selectCode.text(areaCode);
                $('#areaBOX').slideUp();
            });
        });
    }
	// 监听获取手机号区域码
	$('#areaInput').on('input', function() {
        const val = $(this).val();
        getPhonesCodeData({ serchval: val });
	});
    // 下载APP
    $('#download').click(function() {
        window.location.href = '/html/download.html';
    });
    // 复制邀请码
    $('#copy').click(function() {
        if ($("#shareCode").hasClass('not_val')) return;
        $("#shareCode").select();
        if (document.execCommand("Copy","false",null) === true) {
            alert('复制成功！！');
        } else {
            alert('复制失败！！');
        }
    });
    // 切换注册方式
    // $('#toggleType').click(function() {
    //     if (type === 2) {
    //         type = 1;
    //         $(this).text('手机注册');
    //         $('#phoneBox').addClass('none');
    //         $('#mailBox').removeClass('none');
    //     } else {
    //         type = 2;
    //         $(this).text('邮箱注册');
    //         $('#phoneBox').removeClass('none');
    //         $('#mailBox').addClass('none');
    //     }
    // });
    // 如果输入框有@则为邮箱,否则为手机号
    $('#phone').on('input', function() {
        const val = $(this).val();
        // 检测账号里面是否有@来判断是否属于邮箱，随之显示不同图标
        if (val.indexOf('@') > -1) {
            type = 1;
            accountIMG.attr('src', require('../../assets/imgs/regisiter/mail.png'));
            selectCode.fadeOut();
        } else {
            type = 2;
            accountIMG.attr('src', require('../../assets/imgs/regisiter/phone.png'));
            selectCode.fadeIn();
        }
    });
    // 显示隐藏选择手机号区域码
    selectCode.click(function() {
		$('#areaBOX').slideDown();
    });
    // 隐藏选择手机号区域码
    $('#areaBACK').click(function() {
		$('#areaBOX').slideUp();
    });
    // 发送验证码
    $('.code_text').click(function() {
        const phone = $('#phone').val();
        const mail = $('#phone').val();
        let tipsText = '';          // 错误提示
        let code_text = $('.code_text');

        if (!$.$testMobile(phone) && type === 2) {
            tipsText = '请输入正确的手机号码！！';
        } else if (!$.$testMail(mail) && type === 1) {
            tipsText = '请输入正确的邮箱！！';
        } 

        if (tipsText) {
            alert(tipsText);
        }
        
        if (!cannotSend && !tipsText) {
            let countNum = 60;
            tipsText = '';
            cannotSend = true;

            clearInterval(setCount);
            setCount = setInterval(function() {
                if (countNum > 0) {
                    code_text.text('重新发送' + countNum + '秒');
                    code_text.addClass('todo');
                    countNum--;
                } else {
                    clearInterval(setCount);
                    cannotSend = false;
                    code_text.text('获取验证码');
                    code_text.removeClass('todo');
                }
            }, 1000);

            getMessageCode({
                mobileEmail: type === 2 ? areaCode + phone : mail
            }).then(function(res) {
                codeText.code = res.data.toString();
                codeText.phone = phone;
            });
        }
    });
    // 点击注册
    $('#btn').click(function() {
        const phone = $('#phone').val();
        const mail = $('#phone').val();
        const code = $('#code').val();
        const password = $('#password').val();
        let tipsText = '';
        
        if(!$.$testMobile(phone) && type === 2) {
            tipsText = '请输入正确的手机号码！！';
        } else if (!$.$testMail(mail) && type === 1) {
            tipsText = '请输入正确的邮箱！！';
        } else if (!$.$testPassword(password)) {
            tipsText = '密码长度必须在6-16之间，且只能由数字和字母组成！！';
        } else if (!Number(code)) {
            tipsText = '请输入正确的验证码！！';
        } else if (code !== codeText.code || phone !== codeText.phone) {
            tipsText = '验证码不正确！！';
        }

        if (tipsText) {
            alert(tipsText);
        }
        
        if (!tipsText) {
            let jsonData = {
                // mobileEmail: type === 2 ? phone : mail,
                mobileEmail: phone,
                password: password,
                invitationCode: $('#shareCode').val()
            };
            tipsText = '';

            doRegister(jsonData).then(function(res) {
                alert('注册成功！！');
                $('#download').click();
            });
        }
    });
});